import request from '@/utils/request'
import { getProcessKey } from '@/api/act/process/wfMenuProcess'
import store from '@/store'
import { onBeforeProcess } from '@/api/act/process/beforeProcess'

// 查询流程列表
export function listProcess(query) {
  return request({
    url: '/act/process/list',
    method: 'get',
    params: query
  })
}

// 查询流程列表
export function listDataProcess() {
  return request({
    url: '/act/process/listData',
    method: 'get'
  })
}

// 查询我的申请
export function applyList(query) {
  return request({
    url: '/act/process/applyList',
    method: 'get',
    params: query
  })
}

// 查询我的申请
export function allList(query) {
  return request({
    url: '/act/process/allList',
    method: 'get',
    params: query
  })
}

// 终止流程
export function stop(actId, reason) {
  const formData = new FormData()
  formData.append('actId', actId)
  formData.append('reason', reason)
  return request({
    url: '/act/process/stopProcess',
    method: 'post',
    data: formData
  })
}

// 删除流程实例
export function delProcess(deploymentId) {
  return request({
    url: '/act/process/delProcess/' + deploymentId,
    method: 'get'
  })
}

// 删除流程实例
export function batchDelProcess(deploymentId) {
  return request({
    url: '/act/process/batchDelProcess/' + deploymentId,
    method: 'delete'
  })
}

/**
 * 业务节点中启动流程
 * @param processData
 */
export function startProcessByNode(processData, callback) {
  return store.dispatch('app/toggleExtraProcess', {
    visible: true,
    processData,
    callback
  })
}

/**
 * 启动流程
 * @param processData
 */
export function startProcess(processData) {
  return request({
    url: '/act/process/startProcess',
    method: 'post',
    data: processData
  })
}

/**
 * 根据菜单标识启动流程
 * @param processData
 * @param callback
 * @returns {*}
 */
export function startProcessByMenu(processData, callback) {
  if (!processData.processKey) {
    return getProcessKey(processData.menuKey).then(response => {
      processData.processKey = response.data
      store.dispatch('app/toggleExtraProcess', {
        visible: true,
        processData,
        callback
      })
    })
  } else {
    store.dispatch('app/toggleExtraProcess', {
      visible: true,
      processData,
      callback
    })
  }
}

/**
 * 启动流程中的保存按钮. (点击后执行方法)
 * @param menuKey  和后台的流程结束逻辑类似. 判断通过菜单判断
 * @param isStart  是否调用方法
 * @param data     传递的数据
 */
export async function beforeTheProcessStarts(menuKey, isStart, data) {
  try {
    if (isStart === undefined || !isStart) {
      return
    }
    await onBeforeProcess(menuKey, data)
  } catch (e) {
    // console.log(e)
  }
}

/**
 * 终止流程
 * @param data 流程实例ID
 */
export function stopProcess(data) {
  this.$prompt('', window.i18n.t('终止原因'), {
    confirmButtonText: window.i18n.t('确定'),
    cancelButtonText: window.i18n.t('取消'),
    inputErrorMessage: window.i18n.t('终止原因不能为空'),
    inputValidator: (reason) => {
      if (reason === null || reason === '' || reason.trim().length < 1) {
        return window.i18n.t('终止原因不能为空')
      }
      if (reason.length > 200) {
        return window.i18n.t('数据过长，请重新录入')
      }
    }
  }).then(({ value }) => {
    this.loading = true
    var that = this
    stop(data.actId, value).then(response => {
      if (response.code === 200) {
        data.actStatus = 'Z'
        this.msgSuccess(window.i18n.t('终止成功'))
        // 流程结束调用结束后的方法
        const vuePage = require('@/components/Act/ListBtn/index')
        vuePage.default.methods.stopProcessMethod(that, data.actId, data.id)
        this.loading = false
      }
    })
  }).catch(() => {
  })
}

/**
 * 废弃流程
 */
export function processDiscard(dataTable, dataId, actId) {
  const formData = new FormData()
  formData.append('actId', actId)
  formData.append('dataId', dataId)
  formData.append('dataTable', dataTable)
  return request({
    url: '/act/process/processDiscard',
    method: 'post',
    data: formData
  })
}

/**
 * 查询流程XML
 * @param procDefId
 */
export function getXml(procDefId) {
  return request({
    url: '/act/process/getXml/' + procDefId,
    method: 'get'
  })
}

/**
 * 流程启动以后查询流程图片
 * @param actId
 */
export function getPhoto(actId) {
  return request({
    url: '/act/process/getPhoto/' + actId,
    method: 'get'
  })
}

/**
 * 流程启动之前查询流程图片
 * @param actId
 */
export function showFlowChart(processDefinitionKey) {
  return request({
    url: '/act/process/showFlowChart/' + processDefinitionKey,
    method: 'get'
  })
}
/**
 * 流程启动之前查询流程图片
 * @param actId
 */
export function genProcessFormDiagram(processDefinitionKey) {
  return request({
    url: '/act/process/genProcessFormDiagram/' + processDefinitionKey,
    method: 'get'
  })
}

/**
 * 流程启动之前查询流程审批人
 * @param actId
 */
export function showFlowAssignees(processDefinitionKey) {
  return request({
    url: '/act/process/showFlowAssignees/' + processDefinitionKey,
    method: 'get'
  })
}

/**
 * 根据流程实例ID和节点aid获取没有审批过的节点数据
 * @param actId
 */
export function showNodeAssignees(actId, sid) {
  return request({
    url: '/act/process/showNodeAssignees/' + actId + '/' + sid,
    method: 'get'
  })
}

/**
 * 根据流程实例ID和节点aid获取没有审批过的节点数据
 * @param processKey
 * @param sid
 */
export function showNodeFormAssignees(processKey, sid) {
  return request({
    url: '/act/process/showNodeFormAssignees/' + processKey + '/' + sid,
    method: 'get'
  })
}

/**
 * 查询流程详情
 * @param procDefId
 */
export function getProcessInfo(actId) {
  return request({
    url: '/act/process/getProcessInfo/' + actId,
    method: 'get'
  })
}

/**
 * 查询流程审批记录
 * @param actId 流程实例ID
 * @param sid 流程节点ID
 */
export function approvalRecord(query) {
  return request({
    url: '/act/process/approvalRecord',
    method: 'get',
    params: query
  })
}

/**
 * 根据processKey 获取那些节点需要选择审批人
 * @param actId
 */
export function getApplyChoiceByKey(processKey) {
  return request({
    url: '/act/process/getApplyChoiceByKey/' + processKey,
    method: 'get'
  })
}

/**
 * 我的申请催办功能
 */
export function hastenWork(actId) {
  return request({
    url: '/act/process/hastenWork/' + actId,
    method: 'get'
  })
}

/**
 * 流程抄送
 */
export function ccProcess(ccData) {
  return request({
    url: '/act/process/ccProcess',
    method: 'post',
    data: ccData
  })
}

/**
 * 恢复流程
 */
export function restore(actId) {
  return request({
    url: '/act/process/restore/' + actId,
    method: 'get'
  })
}

/**
 * 获取流程图
 */
export function genProcessDiagram(actId) {
  return request({
    url: '/act/process/genProcessDiagram/' + actId,
    method: 'get'
  })
}
